package com.zxl.grocery.modular.crawler.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxl.grocery.core.common.exception.BizExceptionEnum;
import com.zxl.grocery.core.shiro.ShiroKit;
import com.zxl.grocery.core.shiro.ShiroUser;
import com.zxl.grocery.core.util.ToolUtil;
import com.zxl.grocery.kernel.model.exception.ServiceException;
import com.zxl.grocery.modular.crawler.entity.CrawlerSources;
import com.zxl.grocery.modular.crawler.mapper.CrawlerSourcesMapper;
import com.zxl.grocery.modular.crawler.model.CrawlerSourcesDto;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 来源 服务类
 * </p>
 *
 * @author zxl
 * @since 2017-08-31
 */
@Service
public class CrawlerSourcesService extends ServiceImpl<CrawlerSourcesMapper, CrawlerSources> {

    public List<Map<String, Object>> getCrawlerSourceList(String source, String sourceUrl) {
        return this.baseMapper.getCrawlerSourceList(source, sourceUrl);
    }

    public void addCrawlerSources(CrawlerSources crawlerSources) {
        ShiroUser shiroUser = ShiroKit.getUserNotNull();
        crawlerSources.setCreateUserId(shiroUser.getId());
        crawlerSources.setCreateDate(new Date());
        this.save(crawlerSources);
    }

    public void delCrawlerSourcesById(Integer crawlerMgrId) {
        if (ToolUtil.isEmpty(crawlerMgrId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        this.removeById(crawlerMgrId);
    }

    public void editCrawlerSources(CrawlerSourcesDto crawlerSourcesDto) {
        ShiroUser shiroUser = ShiroKit.getUserNotNull();
        CrawlerSources crawlerSources = new CrawlerSources();
        BeanUtil.copyProperties(crawlerSourcesDto, crawlerSources);
        crawlerSources.setUpdateUserId(shiroUser.getId());
        crawlerSources.setModifyDate(new Date());
        this.updateById(crawlerSources);
    }
}
